package ss.lianft.tools;

import org.beetl.core.Configuration;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.kit.GenKit;
import org.beetl.sql.core.kit.StringKit;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;

public class CustomSQLManager extends SQLManager {

    public CustomSQLManager(DBStyle dbStyle, ConnectionSource ds) {
        super(dbStyle, ds);
    }

    public CustomSQLManager(DBStyle dbStyle, SQLLoader sqlLoader, ConnectionSource ds) {
        super(dbStyle, sqlLoader, ds);
    }

    public CustomSQLManager(DBStyle dbStyle, SQLLoader sqlLoader, ConnectionSource ds, NameConversion nc) {
        super(dbStyle, sqlLoader, ds, nc);
    }

    public CustomSQLManager(DBStyle dbStyle, SQLLoader sqlLoader, ConnectionSource ds, NameConversion nc, Interceptor[] inters) {
        super(dbStyle, sqlLoader, ds, nc, inters);
    }

    public CustomSQLManager(DBStyle dbStyle, SQLLoader sqlLoader, ConnectionSource ds, NameConversion nc, Interceptor[] inters, String defaultSchema) {
        super(dbStyle, sqlLoader, ds, nc, inters, defaultSchema);
    }

    public CustomSQLManager(DBStyle dbStyle, SQLLoader sqlLoader, ConnectionSource ds, NameConversion nc, Interceptor[] inters, String defaultSchema, Properties ps) {
        super(dbStyle, sqlLoader, ds, nc, inters, defaultSchema, ps);
    }

    public CustomSQLManager(DBStyle dbStyle, SQLLoader sqlLoader, ConnectionSource ds, NameConversion nc, Interceptor[] inters, String defaultSchema, Properties ps, String name) {
        super(dbStyle, sqlLoader, ds, nc, inters, defaultSchema, ps, name);
    }

    @Override
    public void genSQLFile(String table) throws Exception {
        String path = "/sql";
        if (getSqlLoader() instanceof ClasspathLoader) {
            ClasspathLoader loader = (ClasspathLoader)getSqlLoader();
            path = "/sql";
        }

        String fileName = StringKit.toLowerCaseFirstOne(getNc().getClassName(table));
        String target = GenKit.getJavaResourcePath() + "/" + path + "/" + fileName + ".md";
        FileWriter writer = new FileWriter(new File(target));
        this.genSQLTemplate(table, writer);
        writer.close();
        System.out.println("gen \"" + table + "\" success at " + target);
    }

    private void genSQLTemplate(String table, Writer w) throws IOException {
        String template = null;
        Configuration cf = getBeetl().getGroupTemplate().getConf();
        String hs = cf.getPlaceholderStart();
        String he = cf.getPlaceholderEnd();
        StringBuilder cols = new StringBuilder();
        String sql = "select \n \t@pageTag(){\n\t\t" + hs + "use(\"cols\")" + he + "\n\t@}\n\t from " + table + " where " + hs + "use(\"condition\")" + he;
        cols.append("list").append("\n===\n").append("\n\t").append(sql);
        cols.append("\n");
        cols.append("\ncols").append("\n===\n").append("").append("\n\t").append(getDbStyle().genColumnList(table));
        cols.append("\n");
        cols.append("\nupdate").append("\n===\n").append("").append("\n\t").append(getDbStyle().genColAssignPropertyAbsolute(table));
        cols.append("\n");
        cols.append("\nupdate").append("\n===\n").append("").append("\n\t").append(getDbStyle().genColAssignPropertyAbsolute(table));
        String condition = getDbStyle().genCondition(table);
        condition = condition.replaceAll("\\n", "\n\t");
        String sqlDel = "delete from "+table+" where " + hs + "use(\"condition\")" + he;
        cols.append("\n");
        cols.append("\ndelete").append("\n===\n").append(sqlDel);
        cols.append("\n");
        cols.append("\ncondition").append("\n===\n").append("").append("\n\t").append(condition);
        cols.append("\n");
        w.write(cols.toString());
        w.flush();
    }


}
